package cn.itcast.myapplication.fragment;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.itcast.myapplication.R;


public class HomeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Button button;

        button=getActivity().findViewById(R.id.btn_home);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //通过输入框来传递数据
                EditText editText=getActivity().findViewById(R.id.editText2);

                String data=editText.getText().toString();

                //输入框不能为空传递空白数据
                if (TextUtils.isEmpty(data)){

                    Toast.makeText(getActivity(),"请输入名字！",Toast.LENGTH_SHORT).show();

                    return;

                }

                //通过键和值来传递数据
                final Bundle bundle=new Bundle();
                bundle.putString("my_name",data);

                //当你想切换Fragment时，使用NavController对象，告诉它你想要去Navigation Graph中的哪个Fragment，
                // NavController会将你想去的Fragment展示在NavHostFragment中。
                NavController controller= Navigation.findNavController(view);

                //将值放在这里进行数据的传递
                controller.navigate(R.id.action_homeFragment2_to_detailFragment,bundle);

            }
        });
    }
}